package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.dto.AppRoomDto;
import com.vf.admin.client.service.*;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.fiwork.controller.AppDailyTasController;
import com.vf.admin.miaodong.entity.*;
import com.vf.admin.utils.StringEx;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.vf.admin.client.common.ClientConstants.client_toke;
import static com.vf.admin.client.tx.utils.TxTrtc.jieSanRoom;
import static com.vf.admin.client.tx.utils.TxTrtc.yiChuUser;
import static com.vf.admin.common.Constants.*;
import static com.vf.admin.utils.StringEx.isNull;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppRoomController.java
 * @Description 房间
 * @createTime 2021年07月21日 10:58:0
 */
@CrossOrigin
@Controller
@RequestMapping("/app/room")
public class AppRoomController extends BaseController {
    @Autowired
    AppUserService appUserService;
    @Autowired
    AppSetupService appSetupService;
    @Autowired
    AppRoomTypeService appRoomTypeService;
    @Autowired
    AppRoomImgService appRoomImgService;
    @Autowired
    AppRoomCollectService appRoomCollectService;
    @Autowired
    AppRoomService appRoomService;
    @Autowired
    AppRoomAdminService appRoomAdminService;
    @Autowired
    AppRoomMeiliService appRoomMeiliService;
    @Autowired
    AppFansClubService appFansClubService;
    @Autowired
    AppTaskRecordService appTaskRecordService;
    @Autowired
    AppMingxiRoomService appMingxiRoomService;
    @Autowired
    AppUserDressupService appUserDressupService;
    @Autowired
    AppMountsService appMountsService;
    @Autowired
    AppRoomLaHeiService appRoomLaHeiService;
    @Autowired
    AppRoomJinyanService appRoomJinyanService;
    @Autowired
    AppEarningsService appEarningsService;
    @Autowired
    AppMingXiZuanShiService appMingXiZuanShiService;
    @Autowired
    AppMingxiMeilizhiService appMingxiMeilizhiService;
    @Autowired
    RedisUtil redisUtil;
    @Autowired
    private AppDailyTasController appDailyTasController;
    @Autowired
    private AppAuthUserService appAuthUserService;
    @Autowired
    private AppPrivilegeService appPrivilegeService;

    /**
     * @return com.vf.admin.common.entity.Result
     * @author anxiang
     * @Description 获取语音房间类型
     */
    @RequestMapping(value = "/getYuyinRoomTypeList", method = RequestMethod.POST)
    @ResponseBody
    public Result getYuyinRoomTypeList(HttpServletRequest request) {
        try {
            //获取语音房间类型
            List<AppRoomType> roomTypeList = appRoomTypeService.findList(1);
            return this.sucessJSON(roomTypeList);
        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }

    /**
     * @return com.vf.admin.common.entity.Result
     * @author anxiang
     * @Description 获取视频直播房间类型
     */
    @RequestMapping(value = "/getVideoRoomTypeList", method = RequestMethod.POST)
    @ResponseBody
    public Result getVideoRoomTypeList(HttpServletRequest request) {
        try {
            //获取视频直播房间类型
            List<AppRoomType> roomTypeList = appRoomTypeService.findList(2);
            return this.sucessJSON(roomTypeList);
        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }

    /**
     * @return com.vf.admin.common.entity.Result
     * @author anxiang
     * @Description 获取语音房间背景图片
     */
    @RequestMapping(value = "/getYuyinRoomImgList", method = RequestMethod.POST)
    @ResponseBody
    public Result getYuyinRoomImgList(HttpServletRequest request) {
        try {

            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            //房间背景图片
            List<AppRoomImg> roomImgList = appRoomImgService.OfficialPersonalList(user.getId());
            return this.sucessJSON(roomImgList);
        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }


    /**
     * @param roomid 房间id
     * @param type   操作状态   1收藏  2取消收藏
     * @return com.vf.admin.common.entity.Result
     * @author anxiang
     * @Description 房间收藏/取消收藏
     */
    @RequestMapping(value = "/updateRoomCollect", method = RequestMethod.POST)
    @ResponseBody
    public Result updateRoomCollect(HttpServletRequest request, Integer roomid, Integer type) {
        try {
            if (isNull(roomid)) {
                return errorJSON("房间id不能为空");
            }
            if (isNull(type)) {
                return errorJSON("操作状态不能为空");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);

            //根据动态id获取动态详情-
            AppRoom room = appRoomService.getById(roomid);
            if (StringUtils.isEmpty(room)) {
                return errorJSON("房间不存在");
            }
            AppRoomCollect collect = appRoomCollectService.OfficialPersonalList(user.getId(), roomid);
            if (type == 1) {
                if (StringUtils.isEmpty(collect)) {
                    //房间收藏保存
                    appRoomCollectService.roomCollectSave(user.getId(),room);
                    //增加收藏数量
                    appUserService.roomCollectUpdate(user.getId(), 1);
                    return sucessJSON("收藏成功");
                } else {
                    return errorJSON("您已经收藏了");
                }
            } else if (type == 2) {
                if (!StringUtils.isEmpty(collect)) {
                    appRoomCollectService.del(collect.getId());
                    //减少收藏数量
                    appUserService.roomCollectUpdate(user.getId(), 2);
                    return sucessJSON("取消收藏成功");
                } else {
                    return errorJSON("您还未收藏");
                }
            } else {
                return errorJSON("操作状态不支持");
            }
        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }


    /**
     * 获取房间管理员列表
     *
     * @param pageno 第几页
     * @return
     */
    @RequestMapping("/getTheListOfRoomManagers")
    @ResponseBody
    public Result getTheListOfRoomManagers(Integer pageno, Integer roomid) {
        if (StringEx.isNull(pageno)) {
            return this.errorJSON("pageno不能为空!");
        }
        if (StringEx.isNull(roomid)) {
            return this.errorJSON("roomid不能为空!");
        }
        SplitPageBean splitPageBean = new SplitPageBean(pageno, 20);
        List<AppRoomAdmin> list = appRoomAdminService.adminList(roomid, splitPageBean);
        return this.sucessPage(list, splitPageBean.getNextPageNo());
    }

    /**
     * @param userid 房间id
     * @return com.vf.admin.common.entity.Result
     * @author anxiang
     * @Description 增加房间管理员
     */
    @RequestMapping(value = "/addRoomAdministrator", method = RequestMethod.POST)
    @ResponseBody
    public Result addRoomAdministrator(HttpServletRequest request, Integer userid, Integer roomid) {
        try {
            if (isNull(userid)) {
                return errorJSON("userid不能为空");
            }
            if (isNull(roomid)) {
                return errorJSON("roomid不能为空");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            //对操作权限进行判断
            AppRoom room = appRoomService.getById(roomid);
            if (StringUtils.isEmpty(room)) {
                return errorJSON("房间不存在");
            }
            boolean flag = false;
            //操作用户是否为房主
            if (user.getId().equals(room.getUserid())) {
                flag = true;
            }
            //操作用户是否为管理员
            AppRoomAdmin roomAdmin = appRoomAdminService.getRoom(room.getId(), user.getId());
            if (!StringUtils.isEmpty(roomAdmin)) {
                flag = true;
            }
            if (flag) {
                //判断被操作用户是否已经为管理员
                AppRoomAdmin roomAdmin2 = appRoomAdminService.getRoom(room.getId(), userid);
                if (!StringUtils.isEmpty(roomAdmin2)) {
                    return errorJSON("用户已经是管理员了");
                }
                //判断被操作用户是否为房间房主
                if (userid.equals(room.getUserid())) {
                    return errorJSON("您不能对房主进行操作");
                }
                AppUser admins = appUserService.get(userid);
                AppRoomAdmin admin = new AppRoomAdmin();
                admin.setAdminnick(admins.getNick());
                admin.setAdminphone(admins.getLoginname());
                admin.setAdminpic(admins.getPic());
                admin.setAdminuserid(admins.getId());
                admin.setAdminusercode(admins.getUsercode());
                admin.setAdminsex(admins.getSex());
                admin.setUserid(user.getId());
                admin.setPic(user.getPic());
                admin.setPhone(user.getLoginname());
                admin.setNick(user.getNick());
                admin.setRoomid(room.getId());
                admin.setCreatetime(new Date());
                appRoomAdminService.save(admin);
                return sucessJSON("添加成功");
            } else {
                return errorJSON("您没有权限操作");
            }

        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }

    /**
     * @param userid 房间id
     * @return com.vf.admin.common.entity.Result
     * @author anxiang
     * @Description 移除房间管理员
     */
    @RequestMapping(value = "/removeRoomManager", method = RequestMethod.POST)
    @ResponseBody
    public Result removeRoomManager(HttpServletRequest request, Integer userid, Integer roomid) {
        try {
            if (isNull(userid)) {
                return errorJSON("userid不能为空");
            }
            if (isNull(roomid)) {
                return errorJSON("roomid不能为空");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);

            //对操作权限进行判断
            AppRoom room = appRoomService.getById(roomid);
            if (StringUtils.isEmpty(room)) {
                return errorJSON("房间不存在");
            }
            boolean flag = false;
            //操作用户是否为房主
            if (user.getId().equals(room.getUserid())) {
                flag = true;
            }
            //操作用户是否为管理员
            AppRoomAdmin roomAdmin = appRoomAdminService.getRoom(room.getId(), user.getId());
            if (!StringUtils.isEmpty(roomAdmin)) {
                flag = true;
            }
            if (flag) {
                //判断被操作用户是否已经为管理员
                AppRoomAdmin roomAdmin2 = appRoomAdminService.getRoom(room.getId(), userid);
                if (StringUtils.isEmpty(roomAdmin2)) {
                    return errorJSON("用户还不是管理员");
                }
                //判断被操作用户是否为房间房主
                if (userid.equals(room.getUserid())) {
                    return errorJSON("您不能对房主进行操作");
                }
                appRoomAdminService.del(roomAdmin2.getId());
                return sucessJSON("移除成功");
            } else {
                return errorJSON("您没有权限操作");
            }

        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }


    /**
     * @return com.vf.admin.common.entity.Result
     * @author anxiang
     * @Description 获取房间详情
     */
    @RequestMapping(value = "/getRoomDetail", method = RequestMethod.POST)
    @ResponseBody
    public Result getRoomDetail(Integer roomid) {
        try {
            if (isNull(roomid)) {
                return errorJSON("roomid不能为空");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);

            //对操作权限进行判断
            AppRoom room = appRoomService.getById(roomid);
            if (StringUtils.isEmpty(room)) {
                return errorJSON("房间不存在");
            }
            //迁移至dto
            AppRoomDto ard = new AppRoomDto();
            BeanUtils.copyProperties(room, ard);
            ard.setRoompass(isNull(room.getRoompass())?"":room.getRoompass());
            ard.setLastroomtxcode(user.getLastroomtxcode());
            //查询用户是否收藏过该房间
            AppRoomCollect roomCollect = appRoomCollectService.OfficialPersonalList(user.getId(), room.getId());
            if (StringUtils.isEmpty(roomCollect)) {
                //未查询到记录,未收藏
                ard.setIscollect(2);
            } else {
                //查询到记录,收藏
                ard.setIscollect(1);
            }
            //获取用户在房间的身份
            //判断用户是否和房间创建人是否是同一个人
            if (room.getUserid().equals(user.getId())) {
                //通过，房主
                ard.setRoomrole(room_role_fangzhu);
            } else {
                //不通过
                //判断用户是否是房间管理员
                AppRoomAdmin roomAdmin = appRoomAdminService.getRoom(room.getId(), user.getId());
                if (StringUtils.isEmpty(roomAdmin)) {
                    //未查询到记录, 用户身份
                    ard.setRoomrole(room_role_user);
                } else {
                    //查询到记录, 管理员身份
                    ard.setRoomrole(room_role_admin);
                }
            }
            //获取房间魅力值
            AppRoomMeili roomMeili = appRoomMeiliService.getMeili(user.getId(), room.getId());
            if (StringUtils.isEmpty(roomMeili)) {
                //未查询到记录, 魅力值为0
                ard.setRoommeili(0);
            } else {
                //查询到记录, 魅力值为记录值
                ard.setRoommeili(roomMeili.getMeilizhi());
            }
            //获取粉丝团
            AppFansClub fansClub = appFansClubService.getFensiTuan(user.getId(), room.getRoomcode());
            if (StringUtils.isEmpty(fansClub)) {
                //未查询到记录
                ard.setIsfensi(2);
            } else {
                //查询到记录
                ard.setIsfensi(1);
            }
            //是否拉黑
            AppRoomLaHei laHei = appRoomLaHeiService.getByRoomidAndUserid( room.getId(),user.getId());
            if (StringUtils.isEmpty(laHei)) {
                //未查询到记录
                ard.setIslahei(2);
            } else {
                //查询到记录
                ard.setIslahei(1);
            }
            //是否禁言
            AppRoomJinyan jinyan = appRoomJinyanService.getByRoomidAndUserid( room.getId(),user.getId());
            if (StringUtils.isEmpty(jinyan)) {
                //未查询到记录
                ard.setIsjinyan(2);
            } else {
                //查询到记录
                ard.setIsjinyan(1);
            }
            //获取粉丝团总人数
            List<AppFansClub> appFansClubList = appFansClubService.roomList(roomid, null);
            ard.setFensiCount(appFansClubList.size());
            //先查询redis
            Map<Object, Object> map = redisUtil.hmget("RoomOnline");
            Map<Integer,Integer> onMap=new HashMap<>(16);
            for(Map.Entry<Object, Object> entry : map.entrySet()){
                Object mapKey = entry.getKey();
                Object mapValue = entry.getValue();
                onMap.put(Integer.valueOf(mapKey.toString()),Integer.valueOf(mapValue.toString()));
            }
            int zaixian=0;
            if(onMap.containsKey(room.getTengxuncode())){
                zaixian= Integer.valueOf(onMap.get(room.getTengxuncode()).toString());
            }
            ard.setOnlinenum(zaixian);
            //获取默认坐骑
            AppUserDressup appUserDressup=appUserDressupService.getUserid(user.getId(),1);
            if(StringUtils.isEmpty(appUserDressup)){
                ard.setDefaultzuoqi("");
                ard.setZuoqiname("");
            }else{
                AppMounts mounts=appMountsService.get(appUserDressup.getMountsid());
                ard.setDefaultzuoqi(mounts.getSvgaaddress());
                ard.setZuoqiname(mounts.getGifteffects());
            }
            if(user.getIsguizu() == 1) {
                AppPrivilege appPrivilege = appPrivilegeService.get(user.getNobleid());
                if(appPrivilege != null) {
                    ard.setPrivilegeSvgaAddress(appPrivilege.getSvgaAddress());
                }
            }
            AppSetup setup=appSetupService.get();
            ard.setFensituanmoney(setup.getFensituanmoney());

            appDailyTasController.getAwardOnce(userId, ClientConstants.TaskOnce.WATCH_LIVE_ONCE);
            return this.sucessJSON(ard);
        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }


    /**
     * 开启直播
     *
     * @param request
     * @param roomtitle 房间标题
     * @param image     房间封面
     * @param leixing   房间类型  1语音直播   2视频直播
     * @param pmleixing 排麦方式  1自由上麦  2排麦序
     * @param beijing   房间背景
     * @param gonggao   房间公告
     * @return
     */
    @RequestMapping(value = "/startLiveBroadcast", method = RequestMethod.POST)
    @ResponseBody
    public Result startLiveBroadcast(HttpServletRequest request, String roomtitle, MultipartFile image, Integer leixing, Integer pmleixing, Integer beijing, String gonggao,String lon,String lat) {
        try {
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));

            Integer integer = appAuthUserService.infoByUserAndName(userId, ClientConstants.UserAuth.REAL_AUTH);
            if(integer != 2) {
                //return errorJSON("请先真人认证！");
            }
            if (isNull(roomtitle)) {
                return errorJSON("房间标题不能为空");
            }
            if (isNull(image)) {
                return errorJSON("房间封面不能为空");
            }
            if (isNull(leixing)) {
                //return errorJSON("房间类型不能为空");
            }

            if (isNull(gonggao)) {
                return errorJSON("房间公告不能为空");
            }

            if (isNull(beijing)) {
                return errorJSON("房间背景不能为空");
            }
            if (leixing > 2 || leixing < 1) {
                //return errorJSON("房间类型不支持");
            }
            if (leixing == 1) {
                if (isNull(pmleixing)) {
                    //return errorJSON("排麦方式不能为空");
                }
            }
            //获取最新数据
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            /*if (user.getShipinstate() == 2) {
                return errorJSON("你还没通过视频认证,无法创建房间");
            }
            if (user.getStatus() == 2) {
                return errorJSON("你还没通过身份认证,无法创建房间");
            }*/

            //上传房间封面
            String images = "";
            if (!image.isEmpty()) {
                //判断图片大小
                if (image.getSize() <= UPLOAD_IMG_SIZE_MAX) {
                    //上传阿里云oss
                    String ossUrl = appUserService.aliyunOssupload(image);
                    if (isNull(ossUrl)) {
                        return this.errorJSON("图片上传失败");
                    }
                    images = ossUrl;
                } else {
                    return this.errorJSON("图片大小超过限制！");
                }
            }
            //调用房间创建
            boolean flag = appRoomService.creatRoom(user, leixing, roomtitle, images, gonggao, beijing, pmleixing,lon,lat);
            if (flag) {
                AppRoom room=appRoomService.getUserId(user.getId());
                appRoomService.save(room);
                Map<String, Object> map = new HashMap<>(16);
                map.put("roomcode", user.getTengxuncode());
                map.put("roomid", room.getId());
                return sucessJSON(map, "创建房间成功");
            } else {
                return errorJSON("创建房间失败");
            }
        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }


    /**
     * 更改房间封面
     *
     * @Param: [image ，roomid 房间id]
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/07/2
     * @Time: 14:58
     */
    @RequestMapping("/uproomimg")
    @ResponseBody
    public Result updateRoomFengmian(MultipartFile[] image, Integer roomid) {
        try {
            if (StringEx.isNull(roomid)) {
                return this.errorJSON("房间id不能为空");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);

            //判断房间是否存在
            AppRoom room = appRoomService.getById(roomid);
            if (room == null) {
                return this.errorJSON("房间不存在");
            }
            //判断当前登录人是否是房主
            if (!user.getId().equals(room.getUserid())) {
                return this.errorJSON("暂无权限操作");
            }
            String pic = "";
            //上传：循环获取file数组中得文件
            StringBuffer stringBuffer = new StringBuffer();
            if (image != null && image.length > 0) {
                MultipartFile files = image[0];
                if (!files.isEmpty()) {
                    //判断图片大小
                    if (files.getSize() <= 52428800) {
                        //上传阿里云oss
                        String ossUrl = appUserService.aliyunOssupload(files);
                        if (isNull(ossUrl)) {
                            return errorJSON("图片上传失败");
                        }
                        //拼接
                        stringBuffer.append(ossUrl);
                    } else {
                        return this.errorJSON("图片大小超过限制！");
                    }
                }
            } else {
                return this.errorJSON("图片不能为空");
            }
            pic = stringBuffer.toString();
            appRoomService.uproomimg(roomid, pic);
            return this.sucessJSON(pic, "更改成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }

    /**
     * 更改房间标签类型
     *
     * @Param: [roomid 房间id, typeid 标签类型id]
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/07/2
     * @Time: 15:26
     */
    @RequestMapping("/uproomtype")
    @ResponseBody
    public Result uproomtype(Integer roomid, Integer typeid) {
        try {
            if (StringEx.isNull(roomid)) {
                return this.errorJSON("房间id不能为空");
            }
            if (StringEx.isNull(typeid)) {
                return this.errorJSON("房间类型id不能为空");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            AppRoom room = appRoomService.getById(roomid);
            if (room == null) {
                return this.errorJSON("房间不存在");
            }
            AppRoomType roomType = appRoomTypeService.get(typeid);
            if (roomType == null) {
                return this.errorJSON("未获取到房间类型信息");
            }
            if (!user.getId().equals(room.getUserid())) {
                return this.errorJSON("暂无权限操作");
            }
            appRoomService.uproomtype(roomid, roomType.getId(), roomType.getName(), room.getXingzhi());
            return this.sucessJSON("更改成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }

    /**
     * 更改房间标题
     *
     * @Param: [roomid 房间id, roomname 房间标题]
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/07/2
     * @Time: 15:55
     */
    @RequestMapping("/uproomname")
    @ResponseBody
    public Result uproomname(Integer roomid, String roomname) {
        try {
            if (StringEx.isNull(roomid)) {
                return this.errorJSON("房间id不能为空");
            }
            if (StringEx.isNull(roomname)) {
                return this.errorJSON("房间标题不能为空");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            AppRoom room = appRoomService.getById(roomid);
            if (room == null) {
                return this.errorJSON("房间不存在");
            }
            if (!user.getId().equals(room.getUserid())) {
                return this.errorJSON("暂无权限操作");
            }
            appRoomService.uproomname(roomid, roomname);
            return this.sucessJSON("更改成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }

    /**
     * 更改房间背景
     *
     * @Param: [roomid 房间id,  roombeijingid 房间背景id]
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/07/2
     * @Time: 19:24
     */
    @RequestMapping("/uproombeijing")
    @ResponseBody
    public Result uproombeijing(Integer roomid, Integer roombeijingid) {
        try {
            if (StringEx.isNull(roomid)) {
                return this.errorJSON("房间id不能为空");
            }
            if (StringEx.isNull(roombeijingid)) {
                return this.errorJSON("房间背景id不能为空");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            AppRoom room = appRoomService.getById(roomid);
            if (room == null) {
                return this.errorJSON("房间不存在");
            }
            if (!user.getId().equals(room.getUserid())) {
                return this.errorJSON("暂无权限操作");
            }
            AppRoomImg appRoomImg = appRoomImgService.get(roombeijingid);
            if (appRoomImg == null) {
                return this.errorJSON("该背景图不存在");
            }
            //更改背景图
            appRoomService.uproombeijing(roomid, appRoomImg.getUrl(), appRoomImg.getId());
            return this.sucessJSON("更改成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }

    /**
     * 上传自定义房间背景
     *
     * @Param: [image 图片]
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/07/3
     * @Time: 11:48
     */
    @RequestMapping("/uproombj")
    @ResponseBody
    public Result uproombj(MultipartFile[] image) {
        try {
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);

            String pic = "";
            //上传：循环获取file数组中得文件
            StringBuffer stringBuffer = new StringBuffer();
            if (image != null && image.length > 0) {
                MultipartFile files = image[0];
                if (!files.isEmpty()) {
                    //判断图片大小
                    if (files.getSize() <= 52428800) {
                        //上传阿里云oss
                        String ossUrl = appUserService.aliyunOssupload(files);
                        if (isNull(ossUrl)) {
                            return errorJSON("图片上传失败");
                        }
                        //拼接
                        stringBuffer.append(ossUrl);
                    } else {
                        return this.errorJSON("图片大小超过限制！");
                    }
                }
            } else {
                return this.errorJSON("图片不能为空");
            }
            pic = stringBuffer.toString();
            AppRoomImg appRoomImg = appRoomImgService.getUser(user.getId());
            if (appRoomImg == null) {
                //上传自定义背景图
                appRoomImg = new AppRoomImg();
                appRoomImg.setUserid(user.getId());
                appRoomImg.setCreatetime(new Date());
                appRoomImg.setUrl(pic);
                appRoomImg.setLeixing(2);//个人
                appRoomImgService.save(appRoomImg);
            } else {
                //已存在修改
                appRoomImgService.saveImg(appRoomImg.getId(), pic);
            }
            appRoomImg = appRoomImgService.get(appRoomImg.getId());
            return this.sucessJSON(appRoomImg, "上传成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }


    /**
     * 删除房间自定义背景图
     *
     * @Param: [roomimgid 背景图id]
     * @return: com.vf.admin.common.entity.Result
     * @Author: zhouyunyi
     * @Date: 2021/07/3
     * @Time: 11:58
     */
    @RequestMapping("/delroomimg")
    @ResponseBody
    public Result delroomimg(Integer roomimgid) {
        try {
            if (StringEx.isNull(roomimgid)) {
                return this.errorJSON("背景图id不能为空");
            }
            AppRoomImg roomImg = appRoomImgService.get(roomimgid);
            if (roomImg == null) {
                return this.errorJSON("该背景图不存在");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            if (!user.getId().equals(roomImg.getUserid())) {
                return this.errorJSON("暂无权限操作");
            }
            //删除背景图
            appRoomImgService.del(roomimgid);
            return this.sucessJSON("删除成功");
        } catch (Exception e) {
            return this.errorJSON("系统繁忙");
        }
    }

    /**
     * @param roomid 房间id
     * @return com.vf.admin.common.entity.Result
     * @author anxiang
     * @Description 清空房间魅力值
     */
    @RequestMapping(value = "/removeRoomMeili", method = RequestMethod.POST)
    @ResponseBody
    public Result removeRoomMeili(HttpServletRequest request, Integer roomid) {
        try {
            if (isNull(roomid)) {
                return errorJSON("roomid不能为空");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            //对操作权限进行判断
            AppRoom room = appRoomService.getById(roomid);
            if (StringUtils.isEmpty(room)) {
                return errorJSON("房间不存在");
            }
            boolean flag = false;
            //操作用户是否为房主
            if (user.getId().equals(room.getUserid())) {
                flag = true;
            }
            //操作用户是否为管理员
            AppRoomAdmin roomAdmin = appRoomAdminService.getRoom(room.getId(), user.getId());
            if (!StringUtils.isEmpty(roomAdmin)) {
                flag = true;
            }
            if (flag) {
                //清空房间魅力值
                appRoomMeiliService.delMeili(room.getId());
                return sucessJSON("添加成功");
            } else {
                return errorJSON("您没有权限操作");
            }

        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }


    /**
     * @return com.vf.admin.common.entity.Result
     * @author anxiang
     * @Description 关闭房间
     */
    @RequestMapping(value = "/destroyTheRoom", method = RequestMethod.POST)
    @ResponseBody
    public Result destroyTheRoom(HttpServletRequest request) {
        try {
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            //对操作权限进行判断
            AppRoom room = appRoomService.getUserId(user.getId());
            if (StringUtils.isEmpty(room)) {
                return errorJSON("房间不存在");
            }
            boolean flag = false;
            //操作用户是否为房主
            if (user.getId().equals(room.getUserid())) {
                flag = true;
            }

            if (flag) {
                //销毁房间
                boolean flags = jieSanRoom(room.getTengxuncode());
                if (flags) {
                    return sucessJSON("关闭房间成功");
                } else {
                    return sucessJSON("关闭房间失败");
                }
            } else {
                return errorJSON("您没有权限操作");
            }

        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }

    /**
     * @param roomid 房间id
     * @return com.vf.admin.common.entity.Result
     * @author anxiang
     * @Description 将用户踢出房间
     */
    @RequestMapping(value = "/kickTheUserOutOfTheRoom", method = RequestMethod.POST)
    @ResponseBody
    public Result kickTheUserOutOfTheRoom(HttpServletRequest request, Integer roomid, Integer userid) {
        try {
            if (isNull(roomid)) {
                return errorJSON("roomid不能为空");
            }
            if (isNull(userid)) {
                return errorJSON("userid不能为空");
            }
            //获取最新数据
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            //对操作权限进行判断
            AppRoom room = appRoomService.getById(roomid);
            if (StringUtils.isEmpty(room)) {
                return errorJSON("房间不存在");
            }
            //默认是用户
            int flag = room_role_user;
            //操作用户是否为房主
            if (user.getId().equals(room.getUserid())) {
                //是房主
                flag = room_role_fangzhu;
            } else {
                //操作用户是否为管理员
                AppRoomAdmin roomAdmin = appRoomAdminService.getRoom(room.getId(), user.getId());
                if (!StringUtils.isEmpty(roomAdmin)) {
                    //是管理员
                    flag = room_role_admin;
                }
            }
            //被操作用户是否存在
            AppUser user1 = appUserService.get(userid);
            if (StringUtils.isEmpty(user1)) {
                return errorJSON("该用户不存在");
            }
            //被操作用户和操作用户是否是用一人
            if (user1.getId().equals(user.getId())) {
                return errorJSON("不能对自己进行操作");
            }
            //被操作用户是否为房主
            if (user1.getId().equals(room.getUserid())) {
                return errorJSON("不能对房主进行操作");
            }
            //操作人是管理员
            if (flag == room_role_admin) {
                //被操作用户是否为管理员
                AppRoomAdmin roomAdmin = appRoomAdminService.getRoom(room.getId(), user1.getId());
                if (!StringUtils.isEmpty(roomAdmin)) {
                    //是管理员
                    return errorJSON("不能对管理员进行操作");
                }
            }

            String[] array = new String[1];
            array[0] = user1.getTengxuncode() + "";
            if (flag == room_role_fangzhu || flag == room_role_admin) {
                //踢出房间
                boolean flags = yiChuUser(room.getTengxuncode(), array);
                if (flags) {
                    return sucessJSON("踢出房间成功");
                } else {
                    return sucessJSON("踢出房间失败");
                }
            } else {
                return errorJSON("您没有权限操作");
            }

        } catch (Exception e) {
            return this.errorJSON("系统繁忙，请稍后重试");
        }
    }

    /**
     * 附近房间
     * @param pageno
     * @return
     */
    @RequestMapping(value = "/getNearbyRoom", method = RequestMethod.POST)
    @ResponseBody
    public Result getNearbyRoom(Integer pageno){
        if (pageno == null) {
            return this.errorJSON("页码不存在！");
        }
        Integer userId = (Integer)redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
        //获取当前登录用户的user对象
        AppUser user =appUserService.get(userId);

        if (StringEx.isNull(user.getLon()) || StringEx.isNull(user.getLon())) {
            return this.errorJSON("未获取到当前用户位置信息！");
        }
        //分页数据
        SplitPageBean splitPageBean = new SplitPageBean(pageno, 20);

        List<AppRoom> list = appRoomService.getByNearbyUser(user.getLon(), user.getLat(), splitPageBean);
        return this.sucessJSON(list,"获取成功！");
    }



}
